#!/usr/bin/env bash

table_name=data_10000
table_created=true
raw_data=$HOME/weibo-research/data/raw/dataset_temp.csv
table_columns=(\
    "user_id     varchar(20),"\
    "rootuser_id varchar(20),"\
    "mid         varchar(25),"\
    "rootid      varchar(25),"\
    "rel_time    integer,"\
    "send_ip     varchar(12),"\
    "send_srcport    varchar(5),"\
    "province_name   varchar(40),"\
    "city_name   varchar(40),"\
    "isp_name        varchar(20),"\
    "net_type        smallint,"\
    "client_type     smallint,"\
    "mobile_type     smallint,"\
    "message_type    smallint,"\
    "audit_status    smallint,"\
    "user_fansnum    integer,"\
    "user_friendsnum integer,"\
    "mid_commentnum  integer,"\
    "mid_retweetnum  integer,"\
    "rootid_commentnum   integer,"\
    "rootid_retweetnum   integer,"\
    "client_remark       varchar(200),"\
    "m_text          text,"\
    "is_long         boolean,"\
    "pic_url         varchar(300),"\
    "audio_url       varchar(300),"\
    "video_url       varchar(300),"\
    "sp_type         smallint"\
)
if [ ! $table_created ]; then
    psql <<__EOF
        create table ${table_name} (${table_columns[@]});
__EOF
    exit 0
fi
# Import data
while read line; do
    data=$(echo "$line" | awk -F '\\|\\|\\&\\|\\|' '{print \
        "'\''"$1"'\''""," \
        "'\''"$2"'\''""," \
        "'\''"$3"'\''""," \
        "'\''"$4"'\''""," \
        $5"," \
        "'\''"$6"'\''""," \
        "'\''"$7"'\''""," \
        "'\''"$8"'\''""," \
        "'\''"$9"'\''""," \
        "'\''"$10"'\''""," \
        $11"," \
        $12"," \
        $13"," \
        $14"," \
        $15"," \
        $16"," \
        $17"," \
        $18"," \
        $19"," \
        $20"," \
        $21"," \
        "'\''"$22"'\''""," \
        "'\''"$23"'\''""," \
        "cast("$24" as boolean)," \
        "'\''"$25"'\''""," \
        "'\''"$26"'\''""," \
        "'\''"$27"'\''""," \
        $28}')
    ((k++))
    echo "$k "
    psql <<__EOF
    insert into ${table_name} values ($data);
__EOF
done < $1




